<?php

namespace app\api\model;

use app\admin\model\Express;
use app\exceptions\ApiException;
use think\Db;
use think\Exception;
use think\Model;

/**
 * Order
 */
class Order extends Model
{
    // 表名
    protected $name = 'order';


    public function getOrder($params)
    {
        $type = $params['type'] ?? 0;
        $map = [];
        if ($type) {
            $map['status'] = $type;
        }
        $map['member_id'] = $params['member_id'];
        $list = self::where($map)->order('id desc')
            ->where('status','>',0)
            ->field(
                'id,out_trade_no,image,land_name,massif_name,pay_amount,
            status,username,phone,address,address_detail,pay_time,
            express_name,express_nums,weight,notes,create_time,transaction_id'
            )
            ->paginate(10);

        foreach ($list as &$value) {
            $value->image = env('domain.image_url') . $value->image;
            $value->create_time = date('Y-m-d H:i:s', $value->create_time);
            $value->pay_time = date('Y-m-d H:i:s', $value->pay_time);
        }


        return api_message($list);
    }


    public function getOrderDetail($params)
    {
        $order_id = $params['out_trade_no'] ?? 0;
        $order = self::where(['out_trade_no' => $order_id,'member_id' => $params['member_id']])
            ->field(
                'id,out_trade_no,image,land_name,massif_name,pay_amount,
            status,username,phone,address,address_detail,pay_time,
            express_name,express_nums,weight,notes,create_time,send_time,take_time'
            )->find();
        if (!$order) {
            throw new ApiException('数据不存在');
        }
        $express = (new Express())->where(['delivery_id' =>$order['express_name']])->find();
        $order['express_name'] = $express['delivery_name'] ?? '未知快递';
        $order['image'] = env('domain.image_url') . $order['image'];
        $order['create_time'] = date('Y-m-d H:i:s', $order['create_time']);
        $order['pay_time'] = date('Y-m-d H:i:s', $order['pay_time']);
        $order['send_time'] = date('Y-m-d H:i:s', $order['send_time']);
        $order['take_time'] = date('Y-m-d H:i:s', $order['take_time']);

        return api_message($order);
    }


    public function confirmReceipt($params)
    {
        $order_id = $params['order_id'] ?? '';
        $order = self::where(['id' => $order_id])->find();
        if ($order['status'] != 2) {
            throw new ApiException('操作有误，请刷新后重试');
        }
        try {
            //更新订单
            \think\facade\Db::startTrans();
            self::where(['id' => $order_id])->update(['status' => 3,'take_time' => time()]);
            //更新地块状态
            (new Massif())->where(['id' => $order['massif_id']])->update(['is_lock' => 0]);
            //更新土地数量
            (new Land())->where(['id' => $order['land_id']])->setInc('residue_nums');
            \think\facade\Db::commit();
        } catch (Exception $e) {
            \think\facade\Db::rollback();
            throw new ApiException('操作失败');
        }

        return api_message('', '操作成功');
    }
}